//
//  GPManageContactController.m
//  Keping
//
//  Created by 23 on 2017/12/9.
//  Copyright © 2017年 柯平. All rights reserved.
//
// 管理联系人

#import "GPManageContactController.h"
#import "KSOSAddContactController.h"
#import "KUserData.h"
#import "GPContactRequest.h"

@interface GPManageContactController ()


@property (weak, nonatomic) IBOutlet UISwitch *autoAddSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *allowOtherSwitch;
@property (weak, nonatomic) IBOutlet UIView *blockUserView;
@property (weak, nonatomic) IBOutlet UILabel *blockUserLabel;


@end

@implementation GPManageContactController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    self.title = @"Contacts";
    [self.blockUserView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scanBlockUsers)]];
    
    
    //更新UI
    // 1 获取当前用户的信息
    KPartyView *partyView = [KPartyView unarchive];
    self.autoAddSwitch.on = partyView.autoAddFriends;
    self.allowOtherSwitch.on = partyView.allowOthersToAdd;
    
    //获取当前黑名单的个数
    WEAKSELF
    self.blockUserLabel.text = @"Block User(0)";
    KSOSAddContactController *vc = [[KSOSAddContactController alloc] init];
    vc.vcType = KSOSAddContactVcTypeBlockUsers;
    [vc loadDataWithCallback:^(BOOL success, NSArray<KSosListModel *> *array, NSString *errMsg) {
        if (!success) {
            weakSelf.blockUserLabel.text = @"Block User(0)";
        }else{
            weakSelf.blockUserLabel.text = [NSString stringWithFormat:@"Block User(%zd)", array.count];
        }
    }];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(action) name:@"kRemoveBlackUserSuccessNotification" object:nil];
}

- (void)action {
    WEAKSELF
    KSOSAddContactController *vc = [[KSOSAddContactController alloc] init];
    vc.vcType = KSOSAddContactVcTypeBlockUsers;
    [vc loadDataWithCallback:^(BOOL success, NSArray<KSosListModel *> *array, NSString *errMsg) {
        if (!success) {
            weakSelf.blockUserLabel.text = @"Block User(0)";
        }else{
            weakSelf.blockUserLabel.text = [NSString stringWithFormat:@"Block User(%zd)", array.count];
        }
    }];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - 查看黑名单
- (void)scanBlockUsers
{
    KSOSAddContactController *vc = [[KSOSAddContactController alloc] init];
    vc.vcType = KSOSAddContactVcTypeBlockUsers; 
    [self.navigationController pushViewController:vc animated:YES];
}


- (IBAction)alterAutoAddFriends:(UISwitch *)sender {
    
    BOOL on = sender.isOn;
    NSString *isOn = on?@"Y":@"N";
    GPContactRequest *request = [[GPContactRequest alloc] initWithURL:KUpdateprofileURL];
    request.isPost = YES;
    request.params = @{@"autoAddFriends":isOn};
    [request startWithCompletionBlockWithSuccess:^(__kindof KBaseRequest * _Nonnull request) {
        
        NSDictionary *response = request.responseJSONObject;
        
        if ([response[@"statusCode"] integerValue] != 200) {
            
            [self showErrorText:response[@"msg"] errCode:[response[@"statusCode"] integerValue]];
            
            sender.on = !sender.isOn;
            
            return;
        }
        
        [self showSuccess:@"success"];
        
        //修改当前沙盒中的数据
        KPartyView *partyView = [KPartyView unarchive];
        partyView.autoAddFriends = on;
        [partyView archive];
        
        
    } failure:^(__kindof KBaseRequest * _Nonnull request) {
        
        [self showErrorText:@"failture"];
        sender.on = !sender.isOn;
        
    }];
    
}

- (IBAction)alterAllowOthersAdd:(UISwitch *)sender {
    
    BOOL on = sender.isOn;
    NSString *isOn = on?@"Y":@"N";
    GPContactRequest *request = [[GPContactRequest alloc] initWithURL:KUpdateprofileURL];
    request.isPost = YES;
    request.params = @{@"allowOthersToAdd":isOn};
    [request startWithCompletionBlockWithSuccess:^(__kindof KBaseRequest * _Nonnull request) {
        
        NSDictionary *response = request.responseJSONObject;
        
        if ([response[@"statusCode"] integerValue] != 200) {
            
            [self showErrorText:response[@"msg"] errCode:[response[@"statusCode"] integerValue]];
            
            sender.on = !sender.isOn;
            
            return;
        }
        
        [self showSuccess:@"success"];
        
        //修改当前沙盒中的数据
        KPartyView *partyView = [KPartyView unarchive];
        partyView.autoAddFriends = on;
        [partyView archive];
        
    } failure:^(__kindof KBaseRequest * _Nonnull request) {
        
        [self showErrorText:@"failture"];
        sender.on = !sender.isOn;
        
    }];
    
}


@end
